package net.adst.ui.mainframe.toppanel;

import net.adst.resources.VariableNames;
import net.adst.util.DataManager;
import net.adst.util.GBC;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 1:50
 * To change this template use File | Settings | File Templates.
 */
public class CommentArea extends JPanel {

    private JLabel workDescriptionLabel;
    private JTextArea descriptionText;
    private JScrollPane DesciptionScrollPane;

    public CommentArea() {
        setLayout(new GridBagLayout());
        addTextAreas();
    }

    /**
     * Adds to panel description
     */
    private void addTextAreas() {
        workDescriptionLabel = new JLabel("Пометка:");
        descriptionText = new JTextArea() {
            public void setEnabled(boolean enabled) {
                if (enabled)
                    this.setBackground(Color.WHITE);
                else
                    this.setBackground(SystemColor.control);
                super.setEnabled(enabled);
            }
        };
        descriptionText.setLineWrap(true);
        descriptionText.setFont(UIManager.getDefaults().getFont("TabbedPane.font"));
        descriptionText.setEnabled(false);
        DataManager.setObject(VariableNames.COMMENT_TEXT, descriptionText);
        DesciptionScrollPane = new JScrollPane(descriptionText);
        DesciptionScrollPane.setMaximumSize(new Dimension(330, 55));
        DesciptionScrollPane.setPreferredSize(new Dimension(330, 55));
        DesciptionScrollPane.setMinimumSize(new Dimension(330, 55));
        add(workDescriptionLabel, new GBC(0, 0).setInsets(1, 5, 3, 0).setAnchor(GBC.WEST));
        add(DesciptionScrollPane, new GBC(0, 1).setInsets(0, 5, 11, 5).setWeight(1, 1).setFill(GBC.BOTH));
//        descriptionText.addFocusListener(new FocusListener() {
//            @Override
//            public void focusGained(FocusEvent e) {
//                descriptionText.setForeground(Color.BLACK);
//            }
//
//            @Override
//            public void focusLost(FocusEvent e) {
//            }
//        });
//        descriptionText.getDocument().addDocumentListener(new DocumentListener() {
//            @Override
//            public void insertUpdate(DocumentEvent e) {
//                validateInput();
//            }
//
//            @Override
//            public void removeUpdate(DocumentEvent e) {
//                validateInput();
//            }
//
//            @Override
//            public void changedUpdate(DocumentEvent e) {
//                validateInput();
//            }
//
//            public void validateInput() {
//                JTextField logTimeField = (JTextField) DataManager.getObject(ResourceManager.getVariableName("ui.topPanel.saveArea.logTime.field"));
//                JButton saveButton = (JButton) DataManager.getObject(ResourceManager.getVariableName("ui.topPanel.saveArea.logTime.button"));
//                Matcher matcher = OrsConstants.pattern.matcher(logTimeField.getText().trim());
//                if((OrsTabbedPane.getIndex() == 2) && matcher.matches()){
//                    JTable table = (JTable) DataManager.getObject(ResourceManager.getVariableName("ui.reportsTab.detailedTable"));
//                    int index = table.getSelectionModel().getMinSelectionIndex();
//                    if (index >= 0){
//                        long tm1 = LogTimeFormatter.getTimeInMinutes(logTimeField.getText());
//                        int col = table.getColumn(ResourceManager.getString("ui.reports_tab.DetailedTableArea.headerRep")).getModelIndex();
//                        String time = (String) table.getValueAt(index, col);
//                        long tm2 = LogTimeFormatter.getTimeInMinutes(time);
//                        String comment1 = ((JTextArea) DataManager.getObject(ResourceManager.getVariableName("ui.topPanel.textArea.description"))).getText().trim();
//                        col = table.getColumn(ResourceManager.getString("ui.reports_tab.DetailedTableArea.headerDesc")).getModelIndex();
//                        String comment2 = ((String) table.getValueAt(index, col)).trim();
//                        col = table.getColumn(ResourceManager.getString("ui.reports_tab.DetailedTableArea.headerDate")).getModelIndex();
//                        Calendar cal1 = ((JDateChooser) DataManager.getObject(ResourceManager.getVariableName("ui.topPanel.reportDateArea.task.dateChooser"))).getCalendar();
//                        Calendar cal2 = Calendar.getInstance();
//                        try {
//                            cal2.setTime(((SimpleDateFormat) DateFormat.getDateInstance(OrsConstants.calendarDateFormat)).parse((String) table.getValueAt(index, col)));
//                        } catch (ParseException e) {
//                        }
//                        if ((tm1 == tm2) && (comment1.equals(comment2)) && (TableRefresher.compareDate(cal1, cal2) == 0)) {
//                            saveButton.setEnabled(false);
//                        } else {
//                            saveButton.setEnabled(true);
//                        }
//                    }
//                }
//            }
//        });
    }}
